<?php
if (!defined('ABSPATH')) {
	define('ABSPATH', dirname(dirname(__FILE__)) . '/');
}

require_once ABSPATH . 'dr-config.php';
require_once ABSPATH . DRINC . '/dr-base.php';
require_once ABSPATH . DRINC . '/dr-functions.php';

require_once ABSPATH . DRINC . '/dr-themes.php';
require_once ABSPATH . DRINC . '/mygravatar.php';

require_once ABSPATH . DRINC . '/dr-smarty.php';

require_once ABSPATH . DRINC . '/dr-page-check-login.php';

# 确保开启session
if (!isset($_SESSION)) {
	session_start();
}


if (isset($_GET['type']) && isset($_GET['qid']) && isset($_GET['aid'])) {
	$_SESSION['type'] = dr_get('type');
	$_SESSION['qid'] = dr_get('qid');
	$_SESSION['aid'] = dr_get('aid');
	$_SESSION['location'] = dr_get('location');
}

if (isset($_GET['type']) && $_GET['type'] == 'ask') {
	$smarty->assign("error_info", "请您先登录，然后提问。。。");
	$_SESSION['type'] = dr_get('type');
}

function verify_info()
{
	global $captcha;
	global $username;
	global $password;

	//用户名检查
	if ($username === '') {
		return "用户名不能为空。。。";
	} elseif (!dr_is_username_exist($username)) {
		return "用户名不存在。。。";
	}

	//密码检查
	if ($password === '') {
		return "密码不能为空。。。";
	} elseif (!dr_is_password_valid($password)) {
		return "密码格式不正确：密码长度至少为6";
	}

	//验证码检查
	if ($_SESSION['logintimes'] > 3) {
		if ($captcha === '') {
			return "验证码未填写。。。";
		} elseif (strtolower($captcha) !== strtolower($_SESSION['captcha'])) {
			return "验证码不正确。。。";
		}
	}

	return "pass";
}


# 处理用户登陆请求
if (dr_post("submit") == true) {


	//mail('574649783@qq.com','head','this is body');
	# 记录并设置用户尝试登陆的次数
	if (!isset($_SESSION['logintimes'])) {
		$_SESSION['logintimes'] = 1;
	} else {
		$_SESSION['logintimes']++;
	}

	//获取用户表单传递过来的登录信息
	$username = dr_post('username');
	$password = dr_post('password');
	$captcha = dr_post('captcha');
	$autologin = dr_post('autologin');
	$remember = !empty($autologin) ? true : false;

	$result = verify_info();

	if ($result == "pass") {

		# 判断用户名和密码是否匹配
		if (dr_login($username, $password)) {

			//获取重定向链接
			$redirect = BASE_HOSTPATH . 'index.php';
			if (isset($_SESSION['type']) && isset($_SESSION['qid']) && isset($_SESSION['aid'])) {
				$question_id = $_SESSION['qid'];
				$answer_id = $_SESSION['aid'];
				$type = $_SESSION['type'];
				$location = $_SESSION['location'];
				switch ($type) {
					case 'show_question_vote':
					case 'comment':
						$redirect = BASE_HOSTPATH . "dr-includes/dr-page-question-answer.php?qid=$question_id#$location";
						break;
					case 'questions_vote':
						$redirect = BASE_HOSTPATH . "dr-includes/dr-page-question-list.php#$location";
				}
				unset($_SESSION['type']);
				unset($_SESSION['qid']);
				unset($_SESSION['aid']);
				unset($_SESSION['location']);
			} elseif (isset($_SESSION['type']) && $_SESSION['type'] == 'ask') {
				$type = $_SESSION['type'];
				$redirect = BASE_HOSTPATH . "dr-includes/dr-page-askquestion.php";
				unset($_SESSION['type']);
			}

			$userid = dr_db_user_id($username);
			dr_set_logged_in_user($userid, $username, $remember);

			$smarty->assign('is_login', $userid);

			# 成功登陆后，清空此前的logintime记录
			unset($_SESSION['logintimes']);

			# 跳转到指定的页面
			header("Location: $redirect");
			exit();
		} else {
			$result = "用户名和密码不匹配。。。";
		}
	}

	dr_to_html($username);
	dr_to_html($result);
	$smarty->assign_by_ref('username', $username);
	$smarty->assign('tips', $result);
}

# 显示页面的标题
$smarty->assign('page_title', 'Login - iDaren');


# 根据用户的尝试登陆次数来决定显示验证码
if (isset($_SESSION['logintimes']) && $_SESSION['logintimes'] >= 3) {
	$smarty->assign('need_captcha', 'true');
	$smarty->assign('captcha_img', 'dr-includes/dr-captcha.php');
}


$content = array();
$content['top_navs'] = get_page_top_navigation();
$content['menus'] = get_page_menus();
$content['tabs'] = get_page_tabs('login', '');

$smarty->assign('content', $content);

# 显示登陆页面
$smarty->display('login.tpl');
